<?php

class Ead_ListasimuladoController extends Ead_Controller_Action
{
    private $_table     = 'PUBLICACAO_SIMULADO';
    private $_pk        = 'PLS_ID';
    private $_order_col = 'PLS_DAT DESC, PLS_HOR';
    private $_order     = 'DESC';
    private $_sta_col   = 'PLS_STA';
    private $obj        = '';
    private $form       = '';
    
    
    public function init()
    {
         //if($this->getRequest()->getActionName() == 'edit') $this->form = new Admin_Form_Publicacaoead();       
    }
    
    public function indexAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/ead/listasimulado_list.js?1=2');
        
        $sessao = new Zend_Session_Namespace('Aluno');
        
        $this->view->aluno = $sessao->aluno;
        
        $where = ' AND a.ALU_ID = '.$sessao->aluno['ALU_ID'];
       
        $query = 'SELECT SQL_CACHE p.*, a.ALU_ID, r.* FROM '.$this->_table.' p, PUBLICACAO_SIMULADO_ALUNO a, PUBSIM_RANALU r '
                .' WHERE p.PLS_ID = a.PLS_ID AND p.PLS_ID = r.PLS_ID AND p.PLS_ID = a.PLS_ID '
                .' AND a.ALU_ID = r.ALU_ID AND r.ALU_ID = '.$sessao->aluno['ALU_ID']
                .' AND PLS_STA = 2 AND PLS_STAALU = 1'
                .' '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
        
        $this->db->closeConnection();  
    }
    
    public function editAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        
        //$this->view->form = $this->form;
        if($this->getRequest()->isPost()) { 
            $formData = $this->getRequest()->getPost();
            try {
                if(1){//$this->form->isValid($formData)) {
                    
                    $this->db->beginTransaction();
                    
                    $id = $formData[$this->_pk];//(int) $this->form->getValue($this->_pk);
                    
                    //$data = $formData;//$this->form->getValues(); 
                    //echo '<pre>';print_r($formData);
                        
                    $this->db->delete('PUBLICACAO_SIMULADO_RESPOSTA',$this->_pk.' = ' . $id.' AND ALU_ID = '.$formData['ALU_ID']);
                    if(isset($formData['q']))
                    foreach($formData['q'] as $key => $q){

                        $data = array(
                            'PLG_ID'    => strtoupper($formData['g'][$q]),
                            'PLS_ID'    => $id,
                            'ALU_ID'    => $formData['ALU_ID'],
                            'PLR_QST'   => $q,
                            'PLR_ALT'   => strtoupper($formData['a'][$q]),
                            'DCP_ID'    => strtoupper($formData['d'][$q]),
                        );
                        // desabilitado, agora so por ajax
                        //$this->db->insert('PUBLICACAO_SIMULADO_RESPOSTA',$data);
                    }
                    
                    $this->db->commit();
                    
                    $this->_helper->redirector('index');
                } else {
                    $this->view->data = $formData;
                }
            } catch(Exception $ex) {
                //echo $ex->getMessage();
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->view->data = $formData;
                $this->db->rollBack();
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                
                $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id;
                $data = $this->db->fetchRow($query);
                $this->view->data = $data;
            }
        }
    }
    
}